package com.example.shutdown_v1.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Redis连接测试控制器
 */
@RestController
@RequestMapping("/api/redis")
@Slf4j
@RequiredArgsConstructor
@CrossOrigin(originPatterns = "*")
public class RedisConnectionController {
    
    private final RedisTemplate<String, Object> redisTemplate;
    
    /**
     * 测试Redis连接
     */
    @GetMapping("/test")
    public ResponseEntity<Map<String, Object>> testConnection() {
        try {
            // 测试基本连接
            String testKey = "test_connection_" + System.currentTimeMillis();
            String testValue = "test_value";
            
            // 写入测试
            redisTemplate.opsForValue().set(testKey, testValue);
            
            // 读取测试
            Object result = redisTemplate.opsForValue().get(testKey);
            
            // 删除测试数据
            redisTemplate.delete(testKey);
            
            boolean success = testValue.equals(result);
            
            return ResponseEntity.ok(Map.of(
                "success", success,
                "message", success ? "Redis连接正常" : "Redis读写测试失败",
                "testKey", testKey,
                "writeValue", testValue,
                "readValue", result != null ? result.toString() : "null"
            ));
            
        } catch (Exception e) {
            log.error("Redis连接测试失败", e);
            return ResponseEntity.internalServerError().body(Map.of(
                "success", false,
                "message", "Redis连接失败: " + e.getMessage(),
                "error", e.getClass().getSimpleName()
            ));
        }
    }
    
    /**
     * 手动存储电力测试数据
     */
    @PostMapping("/test-power-data")
    public ResponseEntity<Map<String, Object>> testPowerData() {
        try {
            String merchantId = "default";
            String statusKey = "merchant_power_status:" + merchantId;
            
            // 手动存储测试数据
            Map<String, Object> testData = Map.of(
                "allPowerOff", false,
                "channels", Map.of(
                    "1", true,
                    "2", false
                ),
                "updateTime", java.time.LocalDateTime.now().toString(),
                "channelCount", 2,
                "testFlag", "manual_test"
            );
            
            redisTemplate.opsForHash().putAll(statusKey, testData);
            
            // 读取验证
            Map<Object, Object> stored = redisTemplate.opsForHash().entries(statusKey);
            
            return ResponseEntity.ok(Map.of(
                "success", true,
                "message", "手动存储电力测试数据成功",
                "statusKey", statusKey,
                "storedData", stored,
                "hasData", !stored.isEmpty()
            ));
            
        } catch (Exception e) {
            log.error("手动存储电力数据失败", e);
            return ResponseEntity.internalServerError().body(Map.of(
                "success", false,
                "message", "存储失败: " + e.getMessage()
            ));
        }
    }
    
    /**
     * 检查Redis服务器信息
     */
    @GetMapping("/info")
    public ResponseEntity<Map<String, Object>> getRedisInfo() {
        try {
            // 获取Redis连接信息
            String ping = redisTemplate.getConnectionFactory().getConnection().ping();
            
            return ResponseEntity.ok(Map.of(
                "success", true,
                "ping", ping,
                "message", "PONG".equals(ping) ? "Redis服务器响应正常" : "Redis服务器响应异常"
            ));
            
        } catch (Exception e) {
            log.error("获取Redis信息失败", e);
            return ResponseEntity.internalServerError().body(Map.of(
                "success", false,
                "message", "获取Redis信息失败: " + e.getMessage()
            ));
        }
    }
}
